﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class addStudent : System.Web.UI.Page
{
    public OleDbConnection myConnection;
    public static readonly string sConnectionString = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sConnectionString);
        myConnection.Open();
        if (Session["UserName"] == null && Session["UserNo"] == null)
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
                "alert('登录超时（或未登录），请您重新登录！'); parent.location.href='../login.aspx'</script>", false);
        if (!IsPostBack)
            RadioButtonMan.Checked = true;
    }
    protected void ButtonOK_Click(object sender, EventArgs e)
    {
        string stuNo = UserNo.Text.Trim();
        string stuName = UserName.Text.Trim();
        string stuXq;
        string stuClass = UserClass.Text.Trim();
        if (RadioButtonMan.Checked)
            stuXq = "仙葫";
        else
            stuXq = "明秀";

        string mysql = "select * from StudentTb where sNo='" + stuNo + "'  ";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataReader myReader = mycmd.ExecuteReader();
        if (myReader.Read())
            LabelError.Text = "输入的学号已经存在，请重新输入！";
        else
        {
            string myinsert = "insert into StudentTb (sNo,sName,sXq,sClass) values" +
                "('" + stuNo + "','" + stuName + "','" + stuXq + "','" + stuClass + "')";
            mycmd = new OleDbCommand(myinsert, myConnection);
            mycmd.ExecuteScalar();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('学生信息添加成功！');</script>", false);
            UserNo.Text = "";
            UserName.Text = "";
            RadioButtonMan.Checked = true;
            RadioButtonWoman.Checked = false;
            UserClass.Text = "";
            LabelError.Text = "";
        }
    }
    protected void ButtonReset_Click(object sender, EventArgs e)
    {
        UserNo.Text = "";
        UserName.Text = "";
        RadioButtonMan.Checked = true;
        RadioButtonWoman.Checked = false;
        UserClass.Text = "";
        LabelError.Text = "";
    }
}